import { Component, OnInit } from '@angular/core';
import { ToastService } from 'src/app/providers/toast/toast.service';
import * as constantEN from '../../../providers/ConstantEN.js'
import * as constants from '../../../providers/Constants'
import { Storage } from 'src/app/providers/Storage.js';
import { NavController } from '@ionic/angular';
import { AuthService } from 'src/app/services/auth.service.js';
import { ActivatedRoute, Params } from '@angular/router';
@Component({
  selector: 'app-payment-add-card',
  templateUrl: './payment-add-card.page.html',
  styleUrls: ['./payment-add-card.page.scss'],
})
export class PaymentAddCardPage implements OnInit {
  backCard: any = {
    cardUserName: '', //持卡人姓名
    cardNo: '', //卡号
    endDateTime: '', //有有效期
    cvv: '', //cvv
    cardUserFirstName: '', //持卡人姓;
    cardUserlastName: '', //持卡人名
    cardPhone: '', //持卡人电话;
    cardEmail: '', //持卡人Email
    address: '', //信用卡地址
    addressDesc: '', //详细地址
    country: '', //国家;
    city: '', //城市;
    type: true,
  }
  isAdd: boolean = false;
  itemData: any = null;
  isSave: boolean = false;
  saveBtn: string = constantEN.publicUtils.save;
  constructor(private toast: ToastService, private nav: NavController, private auth: AuthService, private activatedRoute: ActivatedRoute) {
    this.activatedRoute.queryParams.subscribe((params: Params) => {
      if (params.itemData) {
        console.log('params==', params);
        this.isAdd = true;
        this.saveBtn = 'submit'
        this.itemData = JSON.parse(params.itemData);
        this.backCard = JSON.parse(params.itemData);
        this.itemData.city = JSON.parse(params.itemData).country
        this.backCard.city = JSON.parse(params.itemData).country

      }
    });

  }

  ngOnInit() {

  }

  save() {
    if (this.isSave == false) {
      this.isSave = true;

     
      if (!this.backCard.cardUserName) {
        this.toast.show(constantEN.paymentAdd.paymentAddTheName);
      } else if (!this.backCard.cardNo) {
        this.toast.show(constantEN.paymentAdd.paymentAddCode);
      }else if(!this.backCard.MM){
        this.toast.show(constantEN.paymentAdd.paymentAddValidity);
      }else if(!this.backCard.YY){
        this.toast.show(constantEN.paymentAdd.paymentAddValidity);
      } else if (!this.backCard.cvv) {
        this.toast.show(constantEN.paymentAdd.paymentAddCvv);
      } else if (!this.backCard.cardUserlastName) {
        this.toast.show(constantEN.paymentAdd.paymentAddName);
      } else if (!this.backCard.cardUserFirstName) {
        this.toast.show(constantEN.paymentAdd.paymentAddSurName);
      } else if (!this.backCard.address) {
        this.toast.show(constantEN.paymentAdd.paymentAddAddress);
      } else if (!this.backCard.addressDesc) {
        this.toast.show(constantEN.paymentAdd.paymentAddDetailedAddress);
      } else if (!this.backCard.city) {
        this.toast.show(constantEN.paymentAdd.paymentAddCity);
      } else if (!this.backCard.cardPhone) {
        this.toast.show(constantEN.paymentAdd.paymentAddPhone);
      } else if (!this.backCard.cardEmail) {
        this.toast.show(constantEN.paymentAdd.paymentAddEmail);
      } else {
        this.backCard.endDateTime=JSON.stringify(this.backCard.MM)+JSON.stringify(this.backCard.YY);
        this.isSave = false;
        // Storage.sessionStorage.set('backCard',this.backCard);
        // this.toast.show(constantEN.publicUtils.paySucceed);
        let card = this.backCard;
        card.type = card.type == true ? '0' : '1'
        // this.add(card);
        if (this.isAdd) {
          this.updata(card)
        } else {
          this.add(card)
        }
      }
    }
  }

  add(card) {
    this.auth.bindCard({ card: card }).subscribe(res => {
      if (res.retCode == '000000') {
        // this.toast.show("保存成功");
        this.nav.pop();
      } else if (constants.errCode[res.retCode]) {
        if (constants.errCode[res.retCode] != '0') {
          this.toast.show(constants.errCode[res.retCode]);
        }
      } else if (res.retMsg) {
        this.toast.show(res.retMsg);
      } else {
        this.toast.show("exception service routine");
      }
    }, err => {
      console.log('err', err);
    })
  }
  updata(card) {
    this.auth.updateCard({ card: card }).subscribe(res => {
      if (res.retCode == '000000') {
        // this.toast.show("保存成功");
        this.nav.pop();
      } else if (constants.errCode[res.retCode]) {
        if (constants.errCode[res.retCode] != '0') {
          this.toast.show(constants.errCode[res.retCode]);
        }
      } else if (res.retMsg) {
        this.toast.show(res.retMsg);
      } else {
        this.toast.show("exception service routine");
      }
    }, err => {
      console.log('err', err);
    })
  }
}
